﻿using jlynet.core.Library;
using jlynet.core.notebook.model;
using jlynet.core.notebook.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jlynet.core.notebook.myform
{
    public partial class OptionTabForm : Form
    {
        public OptionTabForm()
        {
            InitializeComponent();
            //密码生成
            get_tabPage_passwordGenerator();

            get_tabPage_backup();

            //选项卡改变时的事件
            this.tabControl_OptionTabPanel.Selected += (s, e) =>
            {
                if (e.TabPage == tabPage_conventional){//常规

                }else if (e.TabPage == tabPage_backup){//备份

                }
                else if (e.TabPage == tabPage_safety){//安全

                }
                else if (e.TabPage == tabPage_passwordGenerator){//密码生成

                }
                else if (e.TabPage == tabPage_other){//其他

                }else{
                    //continue;
                    MessageBox.Show("选项错误！");
                    return;
                }
            };
        }

        #region 选项卡--备份
        private void get_tabPage_backup()
        {
            
        }
        private void button_findPath_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #region 选项卡--密码生成
        private void get_tabPage_passwordGenerator()
        {

            //密码生成--大写字母
            this.checkBox_bigLetter.CheckState = CheckState.Checked;
            this.textBox_bigLetter.Text = "QWERTYUIOPASDFGHJKLZXCVBNM";
            this.checkBox_bigLetter.Click += (s, e) => {
                if (this.checkBox_bigLetter.CheckState == CheckState.Checked)
                {//选中
                    this.textBox_bigLetter.Enabled = true;
                    this.textBox_bigLetter.Text = "QWERTYUIOPASDFGHJKLZXCVBNM";
                }
                else if (this.checkBox_bigLetter.CheckState == CheckState.Unchecked)
                {//未选中
                    this.textBox_bigLetter.Enabled = false;
                    this.textBox_bigLetter.Text = "";
                }
            };
            //密码生成--小写字母
            this.checkBox_smallLetter.CheckState = CheckState.Checked;
            this.textBox_smallLetter.Text = "qwertyuiopasdfghjklzxcvbnm";
            this.checkBox_smallLetter.Click += (s, e) => {
                if (this.checkBox_smallLetter.CheckState == CheckState.Checked)
                {//选中
                    this.textBox_smallLetter.Enabled = true;
                    this.textBox_smallLetter.Text = "qwertyuiopasdfghjklzxcvbnm";
                }
                else if (this.checkBox_smallLetter.CheckState == CheckState.Unchecked)
                {//未选中
                    this.textBox_smallLetter.Enabled = false;
                    this.textBox_smallLetter.Text = "";
                }
            };
            //密码生成--数字
            this.checkBox_number.CheckState = CheckState.Checked;
            this.textBox_number.Text = "0123456789";
            this.checkBox_number.Click += (s, e) => {
                if (this.checkBox_number.CheckState == CheckState.Checked)
                {//选中
                    this.textBox_number.Enabled = true;
                    this.textBox_number.Text = "0123456789";
                }
                else if (this.checkBox_number.CheckState == CheckState.Unchecked)
                {//未选中
                    this.textBox_number.Enabled = false;
                    this.textBox_number.Text = "";
                }
            };
            //密码生成--符号
            this.checkBox_symbol.CheckState = CheckState.Checked;
            this.textBox_symbol.Text = "()&*^%$#@!";
            this.checkBox_symbol.Click += (s, e) => {
                if (this.checkBox_symbol.CheckState == CheckState.Checked)
                {//选中
                    this.textBox_symbol.Enabled = true;
                    this.textBox_symbol.Text = "()&*^%$#@!";
                }
                else if (this.checkBox_symbol.CheckState == CheckState.Unchecked)
                {//未选中
                    this.textBox_symbol.Enabled = false;
                    this.textBox_symbol.Text = "";
                }
            };

            this.radioButton_range.Checked = false;
            this.numericUpDown_rangeStart.Value = 1;
            this.numericUpDown_rangeStart.Enabled = false;
            this.numericUpDown_rangeEnd.Value = 6;
            this.numericUpDown_rangeEnd.Enabled = false;
            this.radioButton_range.CheckedChanged += (s, e) => {
                if (this.radioButton_range.Checked)
                {//选中
                    this.numericUpDown_rangeStart.Enabled = true;
                    this.numericUpDown_rangeEnd.Enabled = true;
                }
                else
                {//未选中
                    this.numericUpDown_rangeStart.Enabled = false;
                    this.numericUpDown_rangeEnd.Enabled = false;
                }
            };

            this.radioButton_fixed.Checked = true;
            this.numericUpDown_fixed.Value = 6;
            this.radioButton_fixed.CheckedChanged += (s, e) => {
                if (this.radioButton_fixed.Checked)
                {//选中
                    this.numericUpDown_fixed.Enabled = true;
                }
                else
                {//未选中
                    this.numericUpDown_fixed.Enabled = false;
                }
            };

            INIFileInitialization ini = new INIFileInitialization();
            INIModel iniModel = ini.IniReadSectionValues("passwordGenerator");
            List<keyValue> keyValues = new List<keyValue>();
            keyValues = iniModel.keyValue;
            for (int i = 0; i < keyValues.Count; i++)
            {
                if ("bigLetter".Equals(keyValues[i].name))
                {
                    this.checkBox_bigLetter.CheckState = CheckState.Checked;
                    this.textBox_bigLetter.Enabled = true;
                    this.textBox_bigLetter.Text = keyValues[i].value.ToString();
                }
                else if ("smallLetter".Equals(keyValues[i].name))
                {
                    this.checkBox_smallLetter.CheckState = CheckState.Checked;
                    this.textBox_smallLetter.Enabled = true;
                    this.textBox_smallLetter.Text = keyValues[i].value.ToString();
                }
                else if ("number".Equals(keyValues[i].name))
                {
                    this.checkBox_number.CheckState = CheckState.Checked;
                    this.textBox_number.Enabled = true;
                    this.textBox_number.Text = keyValues[i].value.ToString();
                }
                else if ("symbol".Equals(keyValues[i].name))
                {
                    this.checkBox_symbol.CheckState = CheckState.Checked;
                    this.textBox_symbol.Enabled = true;
                    this.textBox_symbol.Text = keyValues[i].value.ToString();
                }
                else if ("rangeStart".Equals(keyValues[i].name))
                {
                }
                else if ("rangeEnd".Equals(keyValues[i].name))
                {
                }
                else if ("fixed".Equals(keyValues[i].name))
                {
                }
                else
                {
                }
            }
            //StringHelper.IsNullOrEmpty()

        }
        //点击测试
        private void button_test_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 写入密码生成选项卡数据
        /// </summary>
        /// <param name="ini"></param>
        private void set_tabPage_passwordGenerator(INIFileInitialization ini){
            INIModel model = new INIModel();
            model.node = "passwordGenerator";
            List<keyValue> keyValues = new List<keyValue>();

            if (!"".Equals(this.textBox_bigLetter.Text))
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "bigLetter";
                keyValue01.value = this.textBox_bigLetter.Text;
                keyValues.Add(keyValue01);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "bigLetter");
            }
            if (!"".Equals(this.textBox_smallLetter.Text))
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "smallLetter";
                keyValue01.value = this.textBox_smallLetter.Text;
                keyValues.Add(keyValue01);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "smallLetter");
            }
            if (!"".Equals(this.textBox_number.Text))
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "number";
                keyValue01.value = this.textBox_number.Text;
                keyValues.Add(keyValue01);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "number");
            }
            if (!"".Equals(this.textBox_symbol.Text))
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "symbol";
                keyValue01.value = this.textBox_symbol.Text;
                keyValues.Add(keyValue01);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "symbol");
            }
            if (this.radioButton_range.Checked)
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "rangeStart";
                keyValue01.value = this.numericUpDown_rangeStart.Value;
                keyValues.Add(keyValue01);

                keyValue keyValue02 = new keyValue();
                keyValue02.name = "rangeEnd";
                keyValue02.value = this.numericUpDown_rangeEnd.Value;
                keyValues.Add(keyValue02);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "rangeStart");
                ini.delSectionKey("passwordGenerator", "rangeEnd");
            }
            if (this.radioButton_fixed.Checked)
            {
                keyValue keyValue01 = new keyValue();
                keyValue01.name = "fixed";
                keyValue01.value = this.numericUpDown_fixed.Value;
                keyValues.Add(keyValue01);
            }
            else
            {
                ini.delSectionKey("passwordGenerator", "fixed");
            }

            model.keyValue = keyValues;
            ini.initialization(model);
        }
        #endregion


        #region 选项卡--其他
        /// <summary>
        /// 关联
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_association_Click(object sender, EventArgs e)
        {
            Library.SysHelper.SaveReg(Library.SysHelper.GetExecutablePath(), Constant.Suffix);
        }

        /// <summary>
        /// 取消关联
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancelTheAssociation_Click(object sender, EventArgs e)
        {
            Library.SysHelper.DelReg(Constant.Suffix);
        }
        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            TabPage tabPage = this.tabControl_OptionTabPanel.SelectedTab;
            INIFileInitialization ini = new INIFileInitialization();

            if (tabPage == tabPage_conventional)
            {//常规
                ini.IniReadSectionValues2("passwordGenerator");
            }
            else if (tabPage == tabPage_backup)
            {//备份

            }
            else if (tabPage == tabPage_safety)
            {//安全

            }
            else if (tabPage == tabPage_passwordGenerator)
            {//密码生成
                set_tabPage_passwordGenerator(ini);
            }
            else if (tabPage == tabPage_other)
            {//其他

            }
            this.Close();
            this.Dispose();
        }

        

        
    }
}
